﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using WebSocketDemo.Manager;
using WebSocketDemo.Models;

namespace WebSocketDemo.Controllers
{
    public class HomeController : Controller
    {
        public IActionResult Index()
        {
            return View();
        }

        public IActionResult About()
        {
            ViewData["Message"] = "Your application description page.";

            return View();
        }

        public IActionResult Contact()
        {
            ViewData["Message"] = "Your contact page.";

            return View();
        }

        public IActionResult Privacy()
        {
            return View();
        }

        /// <summary>
        /// WebSocket测试页面
        /// </summary>
        /// <returns></returns>
        public IActionResult WebSocket(string id, string to)
        {
            var SendId = string.Empty;
            var ReceiveId = string.Empty;

            if (string.IsNullOrEmpty(id))
            {
                SendId = "100";
            }
            else
            {
                SendId = id;
            }
            if (string.IsNullOrEmpty(to))
            {
                ReceiveId = "100";
            }
            else
            {
                ReceiveId = to;
            }

            ViewBag.SendId = SendId;
            ViewBag.ReceiveId = ReceiveId;

            return View();
        }

        /// <summary>
        /// 获取聊天记录
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult GetMsgList(string id, string to)
        {
            var SendId = string.Empty;
            var ReceiveId = string.Empty;

            if (string.IsNullOrEmpty(id))
            {
                SendId = "100";
            }
            else
            {
                SendId = id;
            }
            if (string.IsNullOrEmpty(to))
            {
                ReceiveId = "100";
            }
            else
            {
                ReceiveId = to;
            }

            // 获取信息详情,只获取收发消息两者之间的内容,顺序排列
            var list = MsgHistoryManager.Singleton.GetMsgList(SendId).Where(x => (x.SenderID == SendId && x.ReceiverID == ReceiveId)
            || (x.SenderID == ReceiveId && x.ReceiverID == SendId)).OrderBy(x => x.ChatTime).ToList();

            // 处理消息对象
            var totalList = list.Select(s => new ChatModel.ChatItemModel
            {
                ID = s.SenderID,
                Content = s.Content,
                ChatTime = s.ChatTime,
                MessageType = s.MessageType,
                NotReadMsg = s.NotReadMsg

            }).ToList();

            var model = new ChatModel
            {
                TotalMsg = totalList
            };

            return new JsonResult(model);
        }

        /// <summary>
        /// 修改某人发来的未读消息为已读
        /// </summary>
        /// <param name="ReceiverID"></param>
        /// <param name="SenderID"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult UpdateReadMsgStatus(string ReceiverID, string SenderID)
        {
            // 修改未读标记未已读
            var list = MsgHistoryManager.Singleton.GetMsgList(ReceiverID).Where(x => x.NotReadMsg == true
            && x.ReceiverID == ReceiverID && x.SenderID == SenderID).ToList();
            list.ForEach(f =>
            {
                f.NotReadMsg = false;
            });
            return Ok();
        }

        [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
        public IActionResult Error()
        {
            return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
        }
    }
}
